package com.example.service.impl;

import com.example.mapper.BaobiaoMapper;
import com.example.mapper.MaterialMapper;
import com.example.pojo.Baobiao;
import com.example.pojo.Emp;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.BaobiaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class BaobiaoServiceImpl implements BaobiaoService {
    @Autowired
    private BaobiaoMapper baobiaoMapper;
    @Autowired
    private MaterialMapper materialMapper;

    @Override
    public void add(Baobiao baobiao) {
        baobiao.setDate(LocalDate.now());
        baobiaoMapper.add(baobiao);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String metrname, String status, LocalDate begin, LocalDate end) {
        Long count = baobiaoMapper.count(metrname,status,begin,end);//总数返回
        Integer start=(page-1)*pageSize;//计算起始位置
        List<Baobiao> baobiaos = baobiaoMapper.page(start, pageSize,metrname,status,begin,end);//该页记录数返回

        PageBean pageBean = new PageBean(count, baobiaos);
        return pageBean;
    }

    @Override
    public int check_m(Baobiao baobiao) {
        if(baobiaoMapper.getNum(baobiao.getMetrname()) >baobiao.getNum()){
            baobiao.setStatus("库存充足");
            baobiaoMapper.update(baobiao);
            return -1;
        }else {
            baobiao.setStatus("库存不足");
            baobiaoMapper.update2(baobiao);
            return baobiao.getNum()-baobiaoMapper.getNum(baobiao.getMetrname());
        }
    }

    @Override
    public void delete(Integer id) {
        baobiaoMapper.delete(id);
    }

    @Override
    public int getCheck(String name, int num) {
        int kucun=materialMapper.getcun(name);
        int res=num-kucun;
        return res;
    }
}
